package cn.myfirefly.utils.mail;

import java.security.GeneralSecurityException;
import java.util.Properties;

import com.sun.mail.util.MailSSLSocketFactory;

import cn.myfirefly.utils.StrUtil;

public class MailConfigInfo {
	/** 发送邮件的服务器的IP */
    private String mailHost;
    /** 发送邮件的服务器的端口 */
    private String mailPort = "465";
    private boolean mailTls = true;
    private boolean mailSsl = true;
    /** 发送邮件的用户名（邮箱全名称） */
    private String username;
    /** 发送邮件的密码 */
    private String password;
    
    /**
     * 获取邮件参数
     * 
     * @return
     * @throws GeneralSecurityException
     */
    public Properties getProperties() throws GeneralSecurityException {
        Properties props = new Properties();
        props.put("mail.smtp.host", getMailHost());
        // Linux下发送邮件失败，加这个参数试试
        props.put("mail.smtp.localhost", getMailHost());
        props.put("mail.smtp.port", getMailPort());
        if (StrUtil.isBlank(password)) {
        	props.put("mail.smtp.auth", "false");
		} else {
			props.put("mail.smtp.auth", "true");
		}
        if (mailTls) {
        	props.put("mail.smtp.starttls.enable", "true");
		} else {
			props.put("mail.smtp.starttls.enable", "false");
		}

        if (mailSsl) {
        	MailSSLSocketFactory sslSF = new MailSSLSocketFactory();
        	sslSF.setTrustAllHosts(true);
        	props.put("mail.smtp.ssl.enable", "true");
        	props.put("mail.smtp.ssl.socketFactory", sslSF);
		} else {
			props.put("mail.smtp.ssl.enable", "false");
		}
        props.put("mail.transport.protocol", "smtp");

        props.put("mail.user", getUsername());
        props.put("mail.password", getPassword());
        return props;
    }

    /**
     * @return the mailHost
     */
    public String getMailHost() {
        return mailHost;
    }

    /**
     * @param mailHost
     *            
     */
    public void setMailHost(String mailHost) {
        this.mailHost = mailHost;
    }

    /**
     * @return the mailPort
     */
    public String getMailPort() {
        return mailPort;
    }

    /**
     * @param mailPort
     *            
     */
    public void setMailPort(String mailPort) {
        this.mailPort = mailPort;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username
     *            
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     * 
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
	public boolean isMailSsl() {
		return mailSsl;
	}

	public void setMailSsl(boolean mailSsl) {
		this.mailSsl = mailSsl;
	}

	public boolean isMailTls() {
		return mailTls;
	}

	public void setMailTls(boolean mailTls) {
		this.mailTls = mailTls;
	}
}
